package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ActorAdapter;
import flc.ast.adapter.TagAdapter;
import flc.ast.bean.ActorBean;
import flc.ast.bean.WatchedBean;
import flc.ast.databinding.ActivityMovieDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MovieDetailActivity extends BaseAc<ActivityMovieDetailBinding> {

    public static StkResBean stkResBean;
    private TagAdapter tagAdapter;
    private ActorAdapter actorAdapter;
    private boolean isWatched, isWantWatch, isWatching;


    @Override
    protected int onCreate() {
        return R.layout.activity_movie_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivMovieDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvWatched.setOnClickListener(this);
        mDataBinding.tvWantWatch.setOnClickListener(this);
        mDataBinding.tvWatching.setOnClickListener(this);

        mDataBinding.rvTagList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        tagAdapter = new TagAdapter();
        mDataBinding.rvTagList.setAdapter(tagAdapter);

        mDataBinding.rvActorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        actorAdapter = new ActorAdapter();
        mDataBinding.rvActorList.setAdapter(actorAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvWatched://已看
                List<WatchedBean> listSp = AppSpUtils.getWatchedList();
                if (listSp == null) listSp = new ArrayList<>();
                if (isWatched) {
                    for (WatchedBean listDatum : listSp) {
                        if (listDatum.getBean().getUrl().equals(stkResBean.getUrl())) {
                            listSp.remove(listDatum);
                            break;
                        }
                    }
                } else {
                    delete(stkResBean);
                    listSp.add(0, new WatchedBean(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")), stkResBean));
                }
                AppSpUtils.saveWatchedList(listSp);
                getWatchedStatus();
                break;
            case R.id.tvWantWatch://想看
                List<StkResBean> listSp1 = AppSpUtils.getWantWatchList();
                if (listSp1 == null) listSp1 = new ArrayList<>();
                if (isWantWatch) {
                    for (StkResBean listDatum : listSp1) {
                        if (listDatum.getUrl().equals(stkResBean.getUrl())) {
                            listSp1.remove(listDatum);
                            break;
                        }
                    }
                } else {
                    delete(stkResBean);
                    listSp1.add(0, stkResBean);
                }
                AppSpUtils.saveWantWatchList(listSp1);
                getWantWatchStatus();
                break;
            case R.id.tvWatching://在看
                List<StkResBean> listSp2 = AppSpUtils.getWatchingList();
                if (listSp2 == null) listSp2 = new ArrayList<>();
                if (isWatching) {
                    for (StkResBean listDatum : listSp2) {
                        if (listDatum.getUrl().equals(stkResBean.getUrl())) {
                            listSp2.remove(listDatum);
                            break;
                        }
                    }
                } else {
                    delete(stkResBean);
                    listSp2.add(0, stkResBean);
                }
                AppSpUtils.saveWatchingList(listSp2);
                getWatchingStatus();
                break;
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(stkResBean.getThumbUrl()).into(mDataBinding.ivDetailImg);
        mDataBinding.tvDetailName.setText(stkResBean.getName());

        String str = new Gson().toJson(stkResBean.getExtra());
        List<ActorBean> list = new ArrayList<>();
        if (str != null && !str.equals("\"\"")) {
            mDataBinding.tvActorList.setVisibility(View.VISIBLE);
            try {
                JSONObject extra = new JSONObject(str);
                mDataBinding.tvDetailDate.setText(extra.optString("releaseDate"));
                JSONArray actorList = extra.optJSONArray("actorList");
                if (actorList != null) {
                    for (int i = 0; i < actorList.length(); i++) {
                        JSONObject item = actorList.getJSONObject(i);
                        if (!item.getString("role").equals("导演")) {
                            list.add(new ActorBean(item.optString("name"), item.optString("img"), item.optString("role")));
                        }
                    }
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        } else {
            mDataBinding.tvActorList.setVisibility(View.GONE);
        }

        List<String> listTag = new ArrayList<>();
        for (String s : stkResBean.getTagNameList()) {
            listTag.add(s);
        }

        //评分
        double score = (double) stkResBean.getScore_total() / (stkResBean.getScore_count() == 0 ? 1 : stkResBean.getScore_count());
        String formattedNumber = String.format("%.2f", score);
        mDataBinding.tvDetailScoreNum.setText(getString(R.string.score_num, stkResBean.getScore_count()));
        mDataBinding.tvDetailScore.setText(formattedNumber);
        if (score > 8) {
            mDataBinding.zzratingbar.setRating(5);
        } else if (score > 6) {
            mDataBinding.zzratingbar.setRating(4);
        } else if (score > 4) {
            mDataBinding.zzratingbar.setRating(3);
        } else if (score > 2) {
            mDataBinding.zzratingbar.setRating(2);
        } else {
            mDataBinding.zzratingbar.setRating(1);
        }

        mDataBinding.tvDetailDesc.setText(stkResBean.getDesc());

        tagAdapter.setList(listTag);

        actorAdapter.setList(list);

        getWatchedStatus();
        getWantWatchStatus();
        getWatchingStatus();
    }

    private void getWatchedStatus() {
        isWatched = false;
        List<WatchedBean> listSp = AppSpUtils.getWatchedList();
        if (listSp != null && listSp.size() > 0) {
            for (WatchedBean watchedBean : listSp) {
                if (watchedBean.getBean().getUrl().equals(stkResBean.getUrl())) {
                    isWatched = true;
                    break;
                }
            }
        }
        if (isWatched) {
            mDataBinding.tvWatched.setBackgroundColor(Color.parseColor("#42FFFFFF"));
            mDataBinding.tvWatched.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            mDataBinding.tvWatched.setBackgroundColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvWatched.setTextColor(Color.parseColor("#0A68FF"));
        }
    }

    private void getWantWatchStatus() {
        isWantWatch = false;
        List<StkResBean> listSp = AppSpUtils.getWantWatchList();
        if (listSp != null && listSp.size() > 0) {
            for (StkResBean bean : listSp) {
                if (bean.getUrl().equals(stkResBean.getUrl())) {
                    isWantWatch = true;
                    break;
                }
            }
        }
        if (isWantWatch) {
            mDataBinding.tvWantWatch.setBackgroundColor(Color.parseColor("#42FFFFFF"));
            mDataBinding.tvWantWatch.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            mDataBinding.tvWantWatch.setBackgroundColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvWantWatch.setTextColor(Color.parseColor("#0A68FF"));
        }
    }

    private void getWatchingStatus() {
        isWatching = false;
        List<StkResBean> listSp = AppSpUtils.getWatchingList();
        if (listSp != null && listSp.size() > 0) {
            for (StkResBean bean : listSp) {
                if (bean.getUrl().equals(stkResBean.getUrl())) {
                    isWatching = true;
                    break;
                }
            }
        }
        if (isWatching) {
            mDataBinding.tvWatching.setBackgroundColor(Color.parseColor("#42FFFFFF"));
            mDataBinding.tvWatching.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            mDataBinding.tvWatching.setBackgroundColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvWatching.setTextColor(Color.parseColor("#0A68FF"));
        }
    }

    private void delete(StkResBean bean) {
        List<WatchedBean> listSpWatched = AppSpUtils.getWatchedList();
        List<StkResBean> listSpWantWatch = AppSpUtils.getWantWatchList();
        List<StkResBean> listSpWatching = AppSpUtils.getWatchingList();
        if (listSpWatched == null) listSpWatched = new ArrayList<>();
        if (listSpWantWatch == null) listSpWantWatch = new ArrayList<>();
        if (listSpWatching == null) listSpWatching = new ArrayList<>();

        //已看
        for (WatchedBean watchedBean : listSpWatched) {
            if (watchedBean.getBean().getUrl().equals(bean.getUrl())) {
                listSpWatched.remove(watchedBean);
                break;
            }
        }
        AppSpUtils.saveWatchedList(listSpWatched);

        //想看
        for (StkResBean resBean : listSpWantWatch) {
            if (resBean.getUrl().equals(bean.getUrl())) {
                listSpWantWatch.remove(resBean);
                break;
            }
        }
        AppSpUtils.saveWantWatchList(listSpWantWatch);

        //在看
        for (StkResBean resBean : listSpWatching) {
            if (resBean.getUrl().equals(bean.getUrl())) {
                listSpWatching.remove(resBean);
                break;
            }
        }
        AppSpUtils.saveWatchingList(listSpWatching);

        getWatchedStatus();
        getWantWatchStatus();
        getWatchingStatus();
    }

}